//
//  BSSBillDetailsLsitViewController.m
//  BSSMarket
//
//  Created by 传铎 on 2020/9/25.
//  Copyright © 2020 BSS. All rights reserved.
//

#import "BSSBillDetailsLsitViewController.h"
#import "BSSBillDetailsLsitTableViewCell.h"
#import "NoDataView.h"
@interface BSSBillDetailsLsitViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView            * headView;
@end

@implementation BSSBillDetailsLsitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.title = @"账单";
//    self.navBar.titleLabel.textColor = colorCode(@"333333");
    // Do any additional setup after loading the view.
    self.view.backgroundColor = colorCode(@"f2f2f2");
//    self.view.backgroundColor = [UIColor whiteColor];
    [self creatUI];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (IsEmpty(self.dataArray)) {
        return 0;
    }
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (IsEmpty(self.dataArray)) {
        return 0;
    }
    NSArray *arr  = self.dataArray[section];
    return  arr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 55.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.00001f)];
    
  
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
//    view.backgroundColor = [UIColor whiteColor];
    UIView *diView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH - 24, 45)];
    diView.backgroundColor = [UIColor whiteColor];
    [view addSubview:diView];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:diView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = diView.bounds;
    maskLayer.path = maskPath.CGPath;
    diView.layer.mask = maskLayer;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-50, 45.00001f)];
    [diView addSubview:label];
    label.text = [self timeConversion:self.dataArray[section][0][@"createtime"]];
    label.textColor = colorCode(@"333333");
    label.font = [UIFont boldSystemFontOfSize:15];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    BSSBillDetailsLsitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BSSBillDetailsLsitTableViewCell" forIndexPath:indexPath];
    BSSBillDetailsLsitTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (!cell) {
        cell = [[BSSBillDetailsLsitTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }
    if ([Format(self.dataArray[indexPath.section][indexPath.row][@"oedertype"])isEqualToString:@"1"]) {
        cell.typeImageV.image = imageNameStr(@"BSSBill_withdrawal");
    }else if ([Format(self.dataArray[indexPath.section][indexPath.row][@"oedertype"])isEqualToString:@"2"]){
        cell.typeImageV.image = imageNameStr(@"BSSBill_refund");
    }else if ([Format(self.dataArray[indexPath.section][indexPath.row][@"oedertype"])isEqualToString:@"3"]){
        cell.typeImageV.image = imageNameStr(@"BSSBill_collection");
    }else{
        cell.typeImageV.image = imageNameStr(@"");
    }
    cell.titleLabel.text = self.dataArray[indexPath.section][indexPath.row][@"name"];
    cell.amountLabel.text = Format(self.dataArray[indexPath.section][indexPath.row][@"amount"]);
    cell.timeLabel.text = [self timeConversionC:self.dataArray[indexPath.section][indexPath.row][@"createtime"]];
    if ([Format(self.dataArray[indexPath.section][indexPath.row][@"withdrawalstatus"])isEqualToString:@"1"]) {
        cell.typeLabel.text = @"提现中";
        cell.typeLabel.textColor = colorCode(@"#BB1B21");
    }else{
        cell.typeLabel.text = @"已完成";
        cell.typeLabel.textColor = colorCode(@"#666666");
    }
    if ([self.dataArray[indexPath.section][indexPath.row][@"amount"] floatValue] >= 0) {
        cell.amountLabel.textColor = colorCode(@"#BB1B21");
    }else{
        cell.amountLabel.textColor = colorCode(@"#333333");
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    if ([self.dataArray[indexPath.section] count] - 1 == indexPath.row) {
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH - 24, 65) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH - 24, 70);
        maskLayer.path = maskPath.CGPath;
        cell.layer.mask = maskLayer;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 10000000)];
    }else{
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH - 24, 70) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(0.1, 0.1)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH - 24, 70);
        maskLayer.path = maskPath.CGPath;
        cell.layer.mask = maskLayer;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        
    }
    
    
    return cell;
}
- (NSString *)timeConversion:(NSString *)time{
    if (IsEmpty(time)) {
        return @"";
    }
    NSString *str;
    if (time.length >=4) {
        str = [NSString stringWithFormat:@"%@年%@",[time substringWithRange:NSMakeRange(0, 4)],[time substringWithRange:NSMakeRange(4, 2)]];
        return str;
    }else{
        return time;
    }
}
- (NSString *)timeConversionC:(NSString *)time{
    if (IsEmpty(time)) {
        return @"";
    }
    NSString *str;
    if (time.length >=10) {
        str = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",[time substringWithRange:NSMakeRange(0, 4)],[time substringWithRange:NSMakeRange(4, 2)],[time substringWithRange:NSMakeRange(6, 2)],[time substringWithRange:NSMakeRange(8, 2)],[time substringWithRange:NSMakeRange(10, 2)]];
        return str;
    }else{
        return time;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
 
}
- (void)creatUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(12, __kNavigationBarHeight__, SCREEN_WIDTH - 24, SCREEN_HEIGHT - __kNavigationBarHeight__ - __kTabbarSafeBottomMargin__)
                                              style:UITableViewStyleGrouped];
//    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 70;
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册cell
 
//    [_tableView registerClass:[BSSBillDetailsLsitTableViewCell class] forCellReuseIdentifier:@"BSSBillDetailsLsitTableViewCell"];
    [self.view addSubview:_tableView];
    //适配
    if (@available(iOS 13.0, *)) {
        [UICollectionView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }else if (@available(iOS 11.0, *)) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.headView   = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 190)];
    
    NoDataView * _noDataView = [[NoDataView alloc] initXGViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 190)];
    _noDataView.backImage.image = [UIImage imageNamed:@"BSS_NoData"];
    _noDataView.titleLabel.text = @"暂时没有数据呐";
    _noDataView.backView.backgroundColor = [UIColor clearColor];
    _noDataView.backgroundColor = [UIColor clearColor];
    [self.headView addSubview:_noDataView];
    WEAKSELF
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf getMessageType:@"new"];
    }];
//
//    self.tableView.mj_footer = [BSSRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf getMessageType:@"more"];
//    }];
    [_tableView.mj_header beginRefreshing];
}
- (void)getMessageType:(NSString *)messageType{
    NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
//    [jsonDic setObject:@"10" forKey:@"pageSize"];
//    [jsonDic setObject:@"1" forKey:@"pageNo"];
//
    if (!IsEmpty(self.storeId)) {
        [jsonDic setValue:self.storeId forKey:@"bizUserId"];
    }
//    if (![messageType isEqualToString:@"new"]) {
//        [jsonDic setObject:[NSString stringWithFormat:@"%zi",self.dataArray.count/10 + 1] forKey:@"pageNo"];
//    }
    WEAKSELF;
    BSSRequest * request = [[BSSRequest alloc] initWithURL:@"" dic:jsonDic];
    [request startRequest:^(BSSRequestStatus requestStatus, NSString * _Nullable message, id  _Nullable responseObject) {
        if (requestStatus == BSSRequestStatusSuccess) {
            if ([messageType isEqualToString:@"new"]) {
                [weakSelf.dataArray removeAllObjects];
                [weakSelf.tableView.mj_header endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            if (!IsEmpty(responseObject[@"data"])) {
                NSArray *arr = responseObject[@"data"];
                NSMutableArray *dataArr = [NSMutableArray array];
                
                for (NSDictionary *dict in arr) {
                    if (![dataArr containsObject:[self timeConversion:dict[@"createtime"]]]) {
                        [dataArr addObject:[self timeConversion:dict[@"createtime"]]];
                    }
                }
                for (int i = 0; i < dataArr.count; i++) {
                    NSMutableArray *jsonArr = [NSMutableArray array];
                    for (int j = 0; j < arr.count; j ++) {
                        if ([dataArr[i] isEqualToString:[self timeConversion:arr[j][@"createtime"]]]) {
                            [jsonArr addObject:arr[j]];
                        }
                    }
                    [self.dataArray addObject:jsonArr];
                }
                
            }
            if (IsEmpty(weakSelf.dataArray)) {
                self.tableView.tableHeaderView = self.headView;
            }else{
                self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.00001)];
            }
            
//            if ([responseObject[@"data"] count] < 10) {
//                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
//                weakSelf.tableView.mj_footer.hidden = YES;
//            }else{
//                [weakSelf.tableView.mj_footer resetNoMoreData];
//                weakSelf.tableView.mj_footer.hidden = NO;
//            }
            [self.tableView reloadData];
        } else {
            if ([messageType isEqualToString:@"new"]) {
                
                [weakSelf.tableView.mj_header endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            [MBProgressHUD showText:message];
            
        }
    }];
    
}


@end
